package com.zkt.practice.server.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.practice.api.common.PageResult;
import com.zkt.practice.api.common.Result;
import com.zkt.practice.api.req.*;
import com.zkt.practice.api.vo.*;
import com.zkt.practice.server.entity.dto.PracticeSetDTO;
import com.zkt.practice.server.entity.dto.PracticeSubjectDTO;
import com.zkt.practice.server.service.PracticeSetService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/23
 * @description 练习套卷controller层
 **/

@RestController
@RequestMapping("/set")
@Slf4j
public class PracticeSetController {
	
	@Resource
	private PracticeSetService practiceSetService;
	
	/**
	 * 获取专项练习内容
	 */
	@GetMapping("getSpecialPracticeContent")
	public Result<List<SpecialPracticeVO>> getSpecialPracticeContent() {
		try {
			List<SpecialPracticeVO> specialPracticeVOList = practiceSetService.getSpecialPracticeContent();
			if (log.isInfoEnabled()) {
				log.info("PracticeSetController.getSpecialPracticeContent.result:{}", JSON.toJSONString(specialPracticeVOList));
			}
			
			return Result.ok(specialPracticeVOList);
		} catch (Exception e) {
			log.error("PracticeSetController.getSpecialPracticeContent.error:{}", e.getMessage(), e);
			return Result.fail("获取专项练习内容失败");
		}
	}
	
	/**
	 * 开始练习
	 */
	@PostMapping(value = "addPractice")
	public Result<PracticeSetVO> addPractice(@RequestBody GetPracticeSubjectListReq req) {
		if (log.isInfoEnabled()) {
			log.info("开始练习入参{}", JSON.toJSONString(req));
		}
		try {
			// 参数校验
			if (Objects.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (CollUtil.isEmpty(req.getAssembleIds())) {
				throw new Exception("标签ids不能为空");
			}
			
			PracticeSubjectDTO dto = new PracticeSubjectDTO();
			dto.setAssembleIds(req.getAssembleIds());
			
			PracticeSetVO practiceSetVO = practiceSetService.addPractice(dto);
			
			if (log.isInfoEnabled()) {
				log.info("开始练习出参{}", JSON.toJSONString(practiceSetVO));
			}
			
			return Result.ok(practiceSetVO);
		} catch (Exception e) {
			log.error("开始练习异常！错误原因{}", e.getMessage(), e);
			return Result.fail("开始练习失败");
		}
	}
	
	/**
	 * 获取练习题
	 */
	@PostMapping(value = "getSubjects")
	public Result<PracticeSubjectListVO> getSubjects(@RequestBody GetPracticeSubjectsReq req) {
		if (log.isInfoEnabled()) {
			log.info("获取练习题入参{}", JSON.toJSONString(req));
		}
		try {
			if (Objects.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getSetId())) {
				throw new Exception("练习id不能为空");
			}
			
			PracticeSubjectListVO list = practiceSetService.getSubjects(req);
			if (log.isInfoEnabled()) {
				log.info("获取练习题出参{}", JSON.toJSONString(list));
			}
			
			return Result.ok(list);
		} catch (Exception e) {
			log.error("获取练习题异常！错误原因{}", e.getMessage(), e);
			return Result.fail("获取练习题失败");
		}
	}
	
	/**
	 * 获取题目详情
	 */
	@PostMapping(value = "getPracticeSubject")
	public Result<PracticeSubjectVO> getPracticeSubject(@RequestBody GetPracticeSubjectReq req) {
		if (log.isInfoEnabled()) {
			log.info("获取题目详情入参{}", JSON.toJSONString(req));
		}
		try {
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getSubjectId())) {
				throw new Exception("题目id不能为空");
			}
			if (ObjUtil.isNull(req.getSubjectType())) {
				throw new Exception("题目类型不能为空");
			}
			
			PracticeSubjectDTO dto = new PracticeSubjectDTO();
			dto.setSubjectId(req.getSubjectId());
			dto.setSubjectType(req.getSubjectType());
			PracticeSubjectVO vo = practiceSetService.getPracticeSubject(dto);
			if (log.isInfoEnabled()) {
				log.info("获取题目详情出参{}", JSON.toJSONString(vo));
			}
			return Result.ok(vo);
		} catch (Exception e) {
			log.error("获取题目详情异常！错误原因{}", e.getMessage(), e);
			return Result.fail("获取题目详情失败");
		}
	}
	
	/**
	 * 获取模拟套题内容
	 */
	@PostMapping(value = "/getPreSetContent")
	public Result<PageResult<PracticeSetVO>> getPreSetContent(@RequestBody GetPreSetReq req) {
		try {
			if (log.isInfoEnabled()) {
				log.info("获取模拟套题内容入参{}", JSON.toJSONString(req));
			}
			
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getOrderType())) {
				throw new Exception("排序类型不能为空");
			}
			if (StrUtil.isBlank(req.getSetName())) {
				throw new Exception("套题名称不能为空");
			}
			if (ObjUtil.isNull(req.getPageInfo())) {
				throw new Exception("分页信息不能为空");
			}
			
			PracticeSetDTO dto = new PracticeSetDTO();
			
			dto.setOrderType(req.getOrderType());
			dto.setPageInfo(req.getPageInfo());
			dto.setSetName(req.getSetName());
			
			PageResult<PracticeSetVO> list = practiceSetService.getPreSetContent(dto);
			if (log.isInfoEnabled()) {
				log.info("获取模拟套题内容出参{}", JSON.toJSONString(list));
			}
			
			return Result.ok(list);
		} catch (Exception e) {
			log.error("获取模拟套题内容异常！错误原因{}", e.getMessage(), e);
			return Result.fail("获取模拟套题内容失败");
		}
	}
	
	/**
	 * 获取未完成的练题内容
	 */
	@PostMapping(value = "/getUnCompletePractice")
	public Result<PageResult<UnCompletePracticeSetVO>> getUnCompletePractice(@RequestBody GetUnCompletePracticeReq req) {
		try {
			if (log.isInfoEnabled()) {
				log.info("获取未完成的练题内容入参{}", JSON.toJSONString(req));
			}
			
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getPageInfo())) {
				throw new Exception("分页信息不能为空");
			}
			
			PageResult<UnCompletePracticeSetVO> list = practiceSetService.getUnCompletePractice(req);
			if (log.isInfoEnabled()) {
				log.info("获取未完成练习内容出参{}", JSON.toJSONString(list));
			}
			
			return Result.ok(list);
		} catch (Exception e) {
			log.error("获取未完成练习内容异常！错误原因{}", e.getMessage(), e);
			return Result.fail("获取未完成练习内容失败");
		}
	}
	
}